package test.javaVM;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Niki
 * @version 创建时间：2017年4月4日 下午4:44:37 
 * 模拟Java堆OOM 
 * vm args -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 */
public class HeapOOM {
    static class OOMObject {
    }

    

    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<OOMObject>();
        while (true) {
            list.add(new OOMObject()) ;
        }
    }
}

/**
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid5368.hprof ...
Heap dump file created [27875109 bytes in 0.190 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  at java.util.Arrays.copyOf(Arrays.java:2245)
  at java.util.Arrays.copyOf(Arrays.java:2219)
  at java.util.ArrayList.grow(ArrayList.java:242)
  at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
  at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
  at java.util.ArrayList.add(ArrayList.java:440)
  at test.javaVM.HeapOOM.main(HeapOOM.java:21)
*/
